<?php
/*
 * Copyright (c) 2009 Tom Smith (tom@takeontom.com).
 * 
 * Distributed under the terms of the GNU Lesser General Public License.
 * 
 * ---------------------------------------------------------------------------------
 * 
 * This file is part of Seraph PHP Framework.
 *
 * Seraph PHP Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Seraph PHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seraph PHP Framework.  If not, see <http://www.gnu.org/licenses/>.
*/


class MySQLiDBResult extends DBResult	{

	protected $position;
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBResult#getFields()
	 */
	public function getFields()	{
		if ($this->getRawResult() instanceof mysqli_result)	{
			$fieldInfo = $this->getRawResult()->fetch_fields();
		} else {
			$fieldInfo = false;
		}
			
		$out = array();
		if (is_array($fieldInfo))	{
			foreach($fieldInfo as $aField)	{
				$out[] = $aField->name;
			}
		}
		
		return $out;
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBResult#getResultArray()
	 */
	public function getResultArray()	{
		//would like to use this, but requires php 5.3
		//return $this->getRawResult()->fetch_all(MYSQLI_ASSOC);
		
		$out  = array();
		foreach($this as $row)	{
			$out[] = $row;
		}
		
		return $out;
	}

	/* (non-PHPdoc)
	 * @see src/framework/DB/DBResult#getRowCount()
	 */
	public function getRowCount()	{
		if ($this->getRawResult() instanceof mysqli_result)	{
			return $this->getRawResult()->num_rows;
		} else {
			return 0;
		}
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBResult#getAffectedRowsCount()
	 */
	public function getAffectedRowsCount()	{
		return $this->getDBConnection()->getConnection()->affected_rows;
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBResult#getFieldCount()
	 */
	public function getFieldCount()	{
		if ($this->getRawResult() instanceof mysqli_result)	{
			return $this->getRawResult()->field_count;
		} else {
			return 0;
		}
	}

	
	/**
	 * @return Iterator
	 */
	public function getIterator()	{
		return new MySQLiDBResultIterator($this);
	}

	public function offsetExists($offset)	{
		if ($this->getRawResult() instanceof mysqli_result)	{
			return ($offset >= 0 && $offset < count($this));	
		}
		return false;
	}
	public function offsetGet($offset)	{
		if ($this->getRawResult() instanceof mysqli_result)	{
			$this->getRawResult()->data_seek($offset);
			return $this->getRawResult()->fetch_assoc();
		}
		return false;
	}
}
?>